Customer = new Object();

var customer_id = null;

var customer_store_1 = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'code', 'title', 'initials', 'surname', 'denotedByInitials', 'nicPassport', 'customerType', 'accountType', 'residential', 'fmId', 'correspondingAddress',
			'generalAddress', 'phone', 'mobileNo', 'fax', 'corresEmail', 'corresFax', 'corresPost', 'corresInst', 'bankInstrution', 'organizationName', 'businessRegNo',
			'businessAddress' ]
});

var joint_applicant_store = Ext.create('Ext.data.Store', {
	fields : [ 'jaName', 'jaNicPassport', 'jaAddress', 'jaPhone', 'jaEmail', 'jaFax' ]
});

var bank_account_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'nameOfBank', 'branch', 'accountNo' ]
});

var email_store = Ext.create('Ext.data.Store', {
	fields : [ 'email' ]
});

var contact_persons_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'name', 'designation', 'phone', 'fax', 'email' ]
});

var fund_manager_store = Ext.create('Ext.data.Store', {
	fields : [ 'id', 'fmCode', 'fmName', 'fmBusinessRegNo', 'fmAddress', 'fmEmail', 'fmFax', 'fmPhone' ]
});

Customer.getCorrespondance = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Correspondance',
		id : 'FS_Correspondance',
		hidden : true,
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			fieldLabel : 'Correspondance',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Post',
				name : 'corresPost',
				inputValue : '1',
				id : 'checkbox1',
				flex : 1
			}, {
				boxLabel : 'E-mail',
				name : 'corresEmail',
				inputValue : '1',
				id : 'checkbox2',
				flex : 1
			}, {
				boxLabel : 'Fax',
				name : 'corresFax',
				inputValue : '1',
				id : 'checkbox3',
				flex : 1
			} ]
		}, {
			xtype : 'fieldcontainer',
			fieldLabel : 'Corresponding Instruction',
			defaultType : 'radiofield',
			layout : 'hbox',
			items : [ {
				boxLabel : 'Customer Only',
				name : 'corresInst',
				inputValue : 'C',
				id : 'rdco',
				flex : 1
			}, {
				boxLabel : 'Fund Manager Only',
				name : 'corresInst',
				inputValue : 'F',
				id : 'rdfo',
				flex : 1
			}, {
				boxLabel : 'Both',
				name : 'corresInst',
				inputValue : 'B',
				id : 'rdbo',
				flex : 1
			} ]
		} ]
	});
};

Customer.getBankAccountDetails = function() {
	var bank_account_grid = null;
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 2,
		autoCancel : false
	});
	bank_account_grid = Ext.create('Ext.grid.Panel', {
		id : 'FS_BankAccountGrid',
		store : bank_account_store,
		anchor : '100%',
		layout : {
			type : 'fit'
		},
		columns : [ {
			header : 'Name of Bank',
			dataIndex : 'nameOfBank',
			flex : 1.5,
			editor : {
				xtype : 'textfield',
				allowBlank : false,
			}
		}, {
			header : 'Branch',
			dataIndex : 'branch',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Account Number',
			dataIndex : 'accountNo',
			flex : 1.5,
			editor : {
				xtype : 'textfield',
				allowBlank : false,
			}
		} ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var form_bank_account = Ext.getCmp('bank_account_form').getForm();
						if (Ext.getCmp('name_of_bank').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the Name of bank.');
						} else if (Ext.getCmp('account_no').getValue() == null) {
							Ext.MessageBox.alert('Error', 'Please specify the Account No.');
						} else if (!form_bank_account.isValid()) {
							Ext.MessageBox.alert('Error', 'Form is Not valid');
						} else {
							bank_account_store.add({
								nameOfBank : Ext.getCmp('name_of_bank').getValue(),
								branch : Ext.getCmp('bank_barnch').getValue(),
								accountNo : Ext.getCmp('account_no').getValue(),
								id : 0
							});
							form_bank_account.reset();
						}
					}
				}
			}, {
				xtype : 'button',
				text : 'Remove',
				listeners : {
					click : function() {
						var selection = bank_account_grid.getView().getSelectionModel().getSelection()[0];
						if (selection) {
							bank_account_store.remove(selection);
						}
					}
				}
			} ]
		} ],
		height : 150,
		plugins : [ rowEditing ]
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Bank Account Details',
		collapsible : true,
		id : 'FS_BankAccountDetails',
		hidden : true,
		defaults : {
			anchor : '80%'
		},
		items : [ {
			xtype : 'form',
			bodyBorder : false,
			frame : true,
			anchor : '100%',
			border : false,
			id : 'bank_account_form',
			fieldDefaults : {
				labelAlign : 'top'
			},
			layout : {
				type : 'hbox'
			},
			items : [ {
				xtype : 'textfield',
				id : 'name_of_bank',
				msgTarget : 'side',
				maxLength : 100,
				fieldLabel : 'Name Of Bank',
				padding : '0 2 0 0',
				flex : 1
			}, {
				xtype : 'textfield',
				fieldLabel : 'Branch',
				padding : '0 2 0 0',
				maxLength : 100,
				id : 'bank_barnch'
			}, {
				xtype : 'textfield',
				fieldLabel : 'Account No.',
				id : 'account_no',
				maxLength : 60,
				padding : '0 2 0 0',
				flex : 1
			} ]
		}, bank_account_grid, {
			xtype : 'textarea',
			fieldLabel : 'Other Bank Instructions',
			name : 'bankInstrution',
			padding : '5 0 0 0',
			height : 50,
			maxLength : 200,
		} ]
	});
};

Customer.getContactDetails = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Contact Details',
		id : 'FS_CONTACTDETAILS',
		hidden : true,
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textarea',
				fieldLabel : 'Corresponding Address<span style="color:red">*</span>',
				flex : 1,
				name : 'correspondingAddress',
				allowBlank : false,
				msgTarget : 'under',
				maxLength : 250,
				padding : '0 5 0 0'
			}, {
				xtype : 'textarea',
				fieldLabel : 'General Address',
				flex : 1,
				maxLength : 250,
				name : 'generalAddress'
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Phone',
				flex : 1,
				name : 'phone',
				maskRe : /[\d]/,
				minLength : 10,
				padding : '0 5 0 0',
				msgTarget : 'under',
			}, {
				xtype : 'textfield',
				fieldLabel : 'Mobile',
				flex : 1,
				name : 'mobileNo',
				maskRe : /[\d]/,
				minLength : 10,
				labelWidth : 50,
				padding : '0 5 0 0',
				msgTarget : 'under',
			}, {
				xtype : 'textfield',
				fieldLabel : 'Fax',
				flex : 1,
				labelWidth : 30,
				name : 'fax',
				maskRe : /[\d]/,
				minLength : 10,
				msgTarget : 'under',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			labelWidth : 100,
			items : [ {
				xtype : 'textfield',
				vtype : 'email',
				fieldLabel : 'E-mail',
				flex : 1,
				blankText : 'Type and press Enter Key',
				msgTarget : 'under',
				padding : '0 5 0 0',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							if (field.isValid() && field.getValue() != "") {
								var recordIndex = email_store.findBy(function(record, id) {
									if (record.get('email') === field.getValue()) {
										return true;
									}
									return false;
								});

								if (recordIndex != -1) {
									Ext.Msg.alert('Error', "Duplicate recode fund");
								} else {
									email_store.add({
										email : field.getValue()
									});
									field.setRawValue('');
								}
							} else {
								Ext.Msg.alert('Error', "Invalid format");
							}
						}
					},
					blur : function(field, The, eOpts) {
						if (field.rawValue != "" || field.rawValue != null) {
							if (field.isValid() && field.getValue() != "") {
								var recordIndex = email_store.findBy(function(record, id) {
									if (record.get('email') === field.getValue()) {
										return true;
									}
									return false;
								});

								if (recordIndex != -1) {
									Ext.Msg.alert('Error', "Duplicate recode fund");
								} else {
									email_store.add({
										email : field.getValue()
									});
									field.setRawValue('');
								}
							} else {
								Ext.Msg.alert('Error', "Invalid format");
							}
						}
					}
				}
			}, {
				xtype : 'grid',
				store : email_store,
				height : 90,
				id : 'email_grid',
				flex : 1,
				columns : [ {
					text : 'E-mail',
					dataIndex : 'email',
					flex : 1
				}, {
					xtype : 'actioncolumn',
					width : 50,
					align : 'center',
					items : [ {
						icon : 'images/Delete.jpg',
						tooltip : 'Delete',
						handler : function(grid, rowIndex, colIndex) {
							var rec = grid.getStore().getAt(rowIndex);
							if (rec) {
								email_store.remove(rec);
							}
						}
					} ]
				} ],
				listeners : {
					selectionchange : function(sm, selectedRecord) {
						if (selectedRecord.length) {
							console.log(this.body, selectedRecord[0].data);
						}
					}
				}
			} ]
		} ]
	});
};

Customer.getIndividualCustomerDtl = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Personal Details',
		id : 'FS_Individual',
		hidden : true,
		defaults : {
			anchor : '100%',
			labelWidth : 118,
			msgTarget : 'side'
		},
		items : [ {
			xtype : 'fieldcontainer',
			combineErrors : true,
			msgTarget : 'under',
			layout : 'hbox',
			fieldLabel : 'Full Name<span style="color:red">*</span>',
			defaults : {
				hideLabel : true
			},
			items : [ {
				width : 50,
				xtype : 'combo',
				mode : 'local',
				value : 'mrs',
				triggerAction : 'all',
				forceSelection : true,
				editable : false,
				padding : '0 5 0 0',
				fieldLabel : 'Title',
				name : 'title',
				displayField : 'name',
				valueField : 'value',
				queryMode : 'local',
				store : Ext.create('Ext.data.Store', {
					fields : [ 'name', 'value' ],
					data : [ {
						name : 'Mr',
						value : 'mr'
					}, {
						name : 'Mrs',
						value : 'mrs'
					}, {
						name : 'Miss',
						value : 'miss'
					} ]
				})
			}, {
				xtype : 'textfield',
				name : 'othername',
				blankText : 'Initials Field is required',
				flex : 0.5,
				maxLength : 10,
				name : 'initials',
				padding : '0 5 0 0',
				emptyText : 'Initials',
				allowBlank : false
			}, {
				xtype : 'textfield',
				name : 'surname',
				maxLength : 100,
				blankText : 'Surname is required',
				flex : 1,
				emptyText : 'Surname',
				allowBlank : false
			} ]
		}, {
			xtype : 'fieldcontainer',
			msgTarget : 'side',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Denoted by Initials',
				flex : 1,
				maxLength : 200,
				name : 'denotedByInitials',
				padding : '0 5 0 0',
				labelWidth : 118,
			}, {
				xtype : 'textfield',
				fieldLabel : 'NIC/ Passport No.<span style="color:red">*</span>',
				flex : 1,
				msgTarget : 'under',
				labelWidth : 118,
				allowBlank : false,
				name : 'nicPassport',
				listeners : {
					blur : function(field, The, eOpts) {
						if (field.rawValue != "" || field.rawValue != null) {
							field.setRawValue(field.rawValue.toLocaleUpperCase());
						}
					}
				}
			} ]
		} ]
	});
};

Customer.getContactPersonDtl = function() {
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 2,
		autoCancel : false
	});
	var grid = Ext.create('Ext.grid.Panel', {
		id : 'contactpersonsgrid',
		store : contact_persons_store,
		layout : {
			type : 'fit'
		},
		height : 135,
		anchor : '100%',
		columns : [ {
			header : 'Name',
			dataIndex : 'name',
			width : 140,
			flex : 1,
			editor : {
				xtype : 'textfield',
				allowBlank : false,
			}
		}, {
			header : 'Designation',
			dataIndex : 'designation',
			width : 140,
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Phone',
			dataIndex : 'phone',
			width : 100,
			editor : {
				xtype : 'textfield',
				allowBlank : false,
			}
		}, {
			header : 'Fax',
			dataIndex : 'fax',
			width : 100,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Email',
			dataIndex : 'email',
			width : 150,
			editor : {
				xtype : 'textfield',
				allowBlank : false,
			}
		} ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var formContactPerson = Ext.getCmp('contactPerson').getForm();
						if (Ext.getCmp('cp_name').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the name.');
						} else if (Ext.getCmp('cp_phone').getValue() == null) {
							Ext.MessageBox.alert('Error', 'Please specify the phone.');
						} else if (Ext.getCmp('cp_email').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the E-mail.');
						} else if (!formContactPerson.isValid()) {
							Ext.MessageBox.alert('Error', 'Form is Not valid');
						} else {
							contact_persons_store.add({
								name : Ext.getCmp('cp_name').getValue(),
								designation : Ext.getCmp('cp_designation').getValue(),
								phone : Ext.getCmp('cp_phone').getValue(),
								fax : Ext.getCmp('cp_fax').getValue(),
								email : Ext.getCmp('cp_email').getValue()
							});
							formContactPerson.reset();
						}
					}
				}
			}, {
				xtype : 'button',
				text : 'Remove',
				listeners : {
					click : function() {
						var selection = grid.getView().getSelectionModel().getSelection()[0];
						if (selection) {
							contact_persons_store.remove(selection);
						}
					}
				}
			} ]
		} ],
		plugins : [ rowEditing ]
	});

	var contaperDtl = Ext.create('Ext.form.FieldSet', {
		title : 'Contact Persons',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'form',
			bodyBorder : false,
			frame : true,
			border : false,
			id : 'contactPerson',

			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				fieldDefaults : {
					labelAlign : 'top'
				},
				items : [ {
					xtype : 'textfield',
					id : 'cp_name',
					msgTarget : 'side',
					fieldLabel : 'Name',
					padding : '0 2 0 0',
					flex : 1
				}, {
					xtype : 'textfield',
					fieldLabel : 'Designation',
					padding : '0 2 0 0',
					id : 'cp_designation',
					flex : 1
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				fieldDefaults : {
					labelAlign : 'top'
				},
				items : [ {
					xtype : 'textfield',
					fieldLabel : 'Phone',
					id : 'cp_phone',
					padding : '0 2 0 0',
					maskRe : /[\d]/,
					minLength : 10,
					msgTarget : 'under',
					flex : 1
				}, {
					xtype : 'textfield',
					fieldLabel : 'Fax',
					id : 'cp_fax',
					padding : '0 2 0 0',
					maskRe : /[\d]/,
					minLength : 10,
					msgTarget : 'under',
					flex : 1
				}, {
					xtype : 'textfield',
					fieldLabel : 'E-mail',
					id : 'cp_email',
					vtype : 'email',
					padding : '0 2 0 0',
					msgTarget : 'under',
					flex : 1
				} ]
			} ]
		}, grid ],
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Contact Detail',
		id : 'FS_ContactPerson',
		hidden : true,
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'Business Address',
			name : 'businessAddress',
			flex : 1.5,
			padding : '0 5 0 0'
		}, contaperDtl ]
	});
};

Customer.getCorporateDetails = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Personal Details',
		id : 'FS_Corporate',
		hidden : true,
		defaults : {
			labelWidth : 175,
			anchor : '100%'
		},
		items : [ {
			xtype : 'textfield',
			fieldLabel : 'Name of Organization<span style="color:red">*</span>',
			maxLength : 200,
			allowBlank : false,
			name : 'organizationName'
		}, {
			xtype : 'textfield',
			fieldLabel : 'Business Registration Number',
			maxLength : 200,
			name : 'businessRegNo',
			anchor : '50%'
		} ]

	});
};

Customer.getJionApplicatDetails = function() {
	var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
		clicksToMoveEditor : 1,
		autoCancel : false
	});
	var jointApplicantgrid = Ext.create('Ext.grid.Panel', {
		layout : {
			type : 'fit'
		},
		anchor : '100%',
		id : 'gridJointAppicant',
		store : joint_applicant_store,
		columns : [ {
			header : 'Name',
			dataIndex : 'jaName',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'NIC /Passprot No.',
			dataIndex : 'jaNicPassport',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Address',
			dataIndex : 'jaAddress',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Telephone',
			dataIndex : 'jaPhone',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		}, {
			header : 'Email',
			dataIndex : 'jaEmail',
			flex : 1,
			editor : {
				xtype : 'textfield',
				vtype : 'email'
			}
		}, {
			header : 'Fax',
			dataIndex : 'jaFax',
			flex : 1,
			editor : {
				xtype : 'textfield'
			}
		} ],
		dockedItems : [ {
			xtype : 'toolbar',
			dock : 'top',
			items : [ '->', {
				xtype : 'button',
				text : 'Add',
				listeners : {
					click : function() {
						var jointapplicantform = Ext.getCmp('jointapplicant_form').getForm();
						if (Ext.getCmp('ja_name').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the name.');
						} else if (Ext.getCmp('ja_nic_passport').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the NIC/ Passport.');
						} else if (Ext.getCmp('ja_email').getValue() == '') {
							Ext.MessageBox.alert('Error', 'Please specify the E-mail.');
						} else if (!jointapplicantform.isValid()) {
							Ext.MessageBox.alert('Error', 'Form is Not valid');
						} else {
							joint_applicant_store.add({
								jaName : Ext.getCmp('ja_name').getValue(),
								jaNicPassport : Ext.getCmp('ja_nic_passport').getValue().toLocaleUpperCase(),
								jaAddress : Ext.getCmp('ja_address').getValue(),
								jaPhone : Ext.getCmp('ja_phone').getValue(),
								jaFax : Ext.getCmp('ja_fax').getValue(),
								jaEmail : Ext.getCmp('ja_email').getValue()
							});
							jointapplicantform.reset();
						}
					}
				}
			}, {
				xtype : 'button',
				text : 'Remove',
				listeners : {
					click : function() {
						var selection = jointApplicantgrid.getView().getSelectionModel().getSelection()[0];
						if (selection) {
							joint_applicant_store.remove(selection);
						}
					}
				}
			} ]
		} ],
		plugins : [ rowEditing ],
		height : 175
	});

	return Ext.create('Ext.form.FieldSet', {
		title : 'Joint Applicant Details',
		collapsible : true,
		id : 'FS_JointAppicantDetails',
		hidden : true,
		defaults : {
			anchor : '80%'
		},
		items : [ {
			xtype : 'form',
			bodyBorder : false,
			frame : true,
			anchor : '100%',
			border : false,
			id : 'jointapplicant_form',
			items : [ {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				fieldDefaults : {
					labelAlign : 'top'
				},
				items : [ {
					xtype : 'textfield',
					id : 'ja_name',
					msgTarget : 'side',
					fieldLabel : 'Name',
					padding : '0 2 0 0',
					flex : 1
				}, {
					xtype : 'textfield',
					fieldLabel : 'Address',
					id : 'ja_address',
					padding : '0 2 0 0',
					flex : 1
				} ]
			}, {
				xtype : 'fieldcontainer',
				layout : 'hbox',
				fieldDefaults : {
					labelAlign : 'top'
				},
				items : [ {
					xtype : 'textfield',
					fieldLabel : 'Nic/ Passport',
					padding : '0 2 0 0',
					id : 'ja_nic_passport',
					flex : 1
				}, {
					xtype : 'textfield',
					id : 'ja_phone',
					msgTarget : 'under',
					fieldLabel : 'Phone',
					padding : '0 2 0 0',
					flex : 1,
					maskRe : /[\d]/,
					minLength : 10
				}, {
					xtype : 'textfield',
					fieldLabel : 'Fax',
					padding : '0 2 0 0',
					id : 'ja_fax',
					flex : 1,
					msgTarget : 'under',
					maskRe : /[\d]/,
					minLength : 10
				}, {
					xtype : 'textfield',
					fieldLabel : 'Email',
					id : 'ja_email',
					msgTarget : 'under',
					vtype : 'email',
					padding : '0 2 0 0',
					flex : 1
				} ]
			} ]
		}, jointApplicantgrid ]

	});

};

Customer.getCommonDetails = function() {
	var accountType = Ext.create('Ext.data.Store', {
		fields : [ 'accountTypeId', 'accountType' ],
		data : [ {
			"accountTypeId" : "I",
			"accountType" : "Individual"
		}, {
			"accountTypeId" : "J",
			"accountType" : "Joint"
		}, {
			"accountTypeId" : "C",
			"accountType" : "Corporate"
		} ]
	});

	var customerType = Ext.create('Ext.data.Store', {
		fields : [ 'customerTypeId', 'customerType' ],
		data : [ {
			"customerTypeId" : "E",
			"customerType" : "External"
		}, {
			"customerTypeId" : "C",
			"customerType" : "Staff of CALI"
		}, {
			"customerTypeId" : "G",
			"customerType" : "Staff of CAL Group"
		} ]
	});
	return Ext.create('Ext.form.FieldSet', {
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Account Type <span style="color:red">*</span>',
				flex : 1,
				msgTarget : 'under',
				valueField : 'accountTypeId',
				store : accountType,
				allowBlank : false,
				blankText : 'Account Type is required',
				displayField : 'accountType',
				queryMode : 'local',
				name : 'accountType',
				emptyText : 'Select a Account Type...',
				padding : '0 5 0 0',
				listeners : {
					select : State_Select,
					change : State_Select
				}
			}, {
				xtype : 'fieldcontainer',
				fieldLabel : 'Residential',
				defaultType : 'radiofield',
				layout : 'hbox',
				flex : 1,
				items : [ {
					boxLabel : 'Local',
					name : 'residential',
					checked : true,
					inputValue : 'L',
					id : 'rdlocal',
					padding : '0 15 0 0'
				}, {
					boxLabel : 'Foreign',
					name : 'residential',
					inputValue : 'F',
					id : 'rdforeign'
				} ]
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				fieldLabel : 'Fund Manager',
				store : fund_manager_store,
				flex : 1,
				displayField : 'fmName',
				valueField : 'id',
				padding : '0 5 0 0',
				autoScroll : true,
				isLoaded : false,
				typeAhead : false,
				name : 'fmId',
				listeners : {
					expand : function(combo, opt) {
						if (!combo.isLoaded) {
							Ext.Ajax.request({
								url : 'getFundManagerListJson.action',
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									combo.getStore().loadRawData(obj.fundManagerBeans);
									combo.isLoaded = true;
								}
							});
						}
					},
					change : function(combo, record, index) {
						if (combo.getValue() == "" || combo.getValue() == "&nbsp;" || combo.getValue == 0)
							combo.setValue(null);
					}
				}
			}, {
				xtype : 'combo',
				flex : 1,
				fieldLabel : 'Customer Type',
				store : customerType,
				valueField : 'customerTypeId',
				queryMode : 'local',
				value : 'E',
				name : 'customerType',
				displayField : 'customerType'
			} ]
		} ]
	});
};

Customer.getCenterPanel = function() {

	var commondetails = Customer.getCommonDetails();
	var individual = Customer.getIndividualCustomerDtl();
	var contactDetails = Customer.getContactDetails();
	var corporate = Customer.getCorporateDetails();
	var jointAppicant = Customer.getJionApplicatDetails();
	var bankAccountDetail = Customer.getBankAccountDetails();
	var contatctPersonDetails = Customer.getContactPersonDtl();
	var correspondance = Customer.getCorrespondance();

	return formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'Customer',
		url : 'addFundManager.action',
		titleAlign : 'left',
		id : 'customer_form',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			labelWidth : 175,
			anchor : '65%'
		},
		fieldDefaults : {
			labelAlign : 'left',
			labelWidth : 150,
		},
		items : [ {
			xtype : 'fieldcontainer',
			items : [ {
				xtype : 'triggerfield',
				emptyText : 'Search Customer',
				triggerCls : 'x-form-search-trigger',
				width : 300,
				name : 'code',
				style : 'float:right;',
				listeners : {
					specialkey : function(field, e) {
						if (e.getKey() == e.ENTER) {
							Ext.MessageBox.show({
								title : 'Customer',
								msg : 'Invalid Code',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.INFO
							});
						}
					}
				},
				onTriggerClick : function() {
					var window = null;
					Ext.Ajax.request({
						url : 'getCustomer.action',
						success : function(response) {
							var obj = Ext.decode(response.responseText);
							customer_store_1.loadRawData(obj.customerBeans);
						}
					});
					if (!window) {
						window = Ext.create('Ext.window.Window', {
							title : 'Customer',
							height : 330,
							frame : true,
							width : 500,
							modal : true,
							constrain : true,
							resizable : false,
							layout : {
								type : 'fit'
							},
							items : [ {
								xtype : 'gridpanel',
								defaults : {
									anchor : '100%'
								},
								store : customer_store_1,
								columns : [ {
									text : 'Customer Code',
									dataIndex : 'code',
									flex : 1
								}, {
									text : 'Customer Name',
									dataIndex : 'surname',
									flex : 1
								}, {
									text : 'Account Type',
									dataIndex : 'accountType'
								}, {
									text : 'Customer Type',
									dataIndex : 'customerType'
								} ],
								listeners : {
									itemdblclick : function(dv, record, item, index, e) {
										Customer.setDataToForm(record);
										window.hide();
									}
								}
							} ]
						});
					}
					if (window.isVisible()) {
						window.hide();
					} else {
						window.show();
					}
				}
			} ]
		}, commondetails, individual, contactDetails, corporate, contatctPersonDetails, correspondance, jointAppicant, bankAccountDetail ],

		// Reset and Submit buttons
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.getCmp('FS_Individual').hide();
				Ext.getCmp('FS_CONTACTDETAILS').hide();
				Ext.getCmp('FS_Corporate').hide();
				Ext.getCmp('FS_BankAccountDetails').hide();
				Ext.getCmp('FS_JointAppicantDetails').hide();
				Ext.getCmp('FS_BankAccountGrid').hide();
				Ext.getCmp('FS_ContactPerson').hide();
				Ext.getCmp('FS_Correspondance').hide();
				email_store.removeAll();
				bank_account_store.removeAll();
				joint_applicant_store.removeAll();
				Ext.getCmp('save').setText('Save');
				customer_id = null;
			}
		}, {
			text : 'Save',
			id : 'save',
			handler : function() {
				var form = this.up('form').getForm();
				if (form.isValid()) {
					formPanel.setLoading('Loading...');
					var email_grids = Ext.getCmp('email_grid');
					var emails = null;
					console.log(email_grids.getStore().getCount());
					if (email_grids.getStore().getCount() > 0) {
						email_grids.getStore().each(function(record) {
							if (emails == null) {
								emails = record.get('email');
							} else {
								emails = emails + "," + record.get('email');
							}
						});
					}
					var bankAccountgrids = Ext.getCmp('FS_BankAccountGrid');
					var acconts = [];
					bankAccountgrids.getStore().each(function(record) {
						var obj = {
							"id" : record.get("id"),
							"accountNo" : record.get("accountNo"),
							"branch" : record.get("branch"),
							"nameOfBank" : record.get("nameOfBank")
						};
						acconts.push(obj);
					});
					var jointapplicant = Ext.getCmp('gridJointAppicant');
					var jointcustomers = [];
					jointapplicant.getStore().each(function(record) {
						var obj = {
							"jaName" : record.get("jaName"),
							"jaNicPassport" : record.get("jaNicPassport"),
							"jaAddress" : record.get("jaAddress"),
							"jaPhone" : record.get('jaPhone'),
							"jaEmail" : record.get('jaEmail'),
							"jaFax" : record.get('jaFax')
						};
						jointcustomers.push(obj);
					});
					var contactpersons_form = Ext.getCmp('contactpersonsgrid');
					var con_person = [];
					contactpersons_form.getStore().each(function(record) {
						var obj = {
							"name" : record.get("name"),
							"designation" : record.get("designation"),
							"phone" : record.get("phone"),
							"fax" : record.get('fax'),
							"email" : record.get('email')
						};
						con_person.push(obj);
					});
					if (customer_id == null) {
						form.url = 'addCustomer.action';
						form.submit({
							params : {
								"emails" : Ext.encode(emails),
								"bankAccounts" : Ext.encode(acconts),
								"jointApplicans" : Ext.encode(jointcustomers),
								"contactPersons" : Ext.encode(con_person)
							},
							success : function(form, action) {
								form.reset();
								email_grids.getStore().removeAll();
								bankAccountgrids.getStore().removeAll();
								joint_applicant_store.removeAll();
								contact_persons_store.removeAll();
								Ext.Msg.alert('Success', 'Success Save <br> Customer Code is ' + action.result.code);
								formPanel.setLoading(false);
								customer_id = null;
								Ext.getCmp('save').setText('Save');
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', action.result.resultMsg);
								formPanel.setLoading(false);
							}
						});
					} else {
						form.url = 'customerUpdate.action';
						form.submit({
							params : {
								"emails" : Ext.encode(emails),
								"bankAccounts" : Ext.encode(acconts),
								"jointApplicans" : Ext.encode(jointcustomers),
								"contactPersons" : Ext.encode(con_person),
								"customerId" : Ext.encode(customer_id)
							},
							success : function(form, action) {
								form.reset();
								email_grids.getStore().removeAll();
								bankAccountgrids.getStore().removeAll();
								Ext.Msg.alert('Success', 'Updated Successfully');
								formPanel.setLoading(false);
								customer_id = null;
								Ext.getCmp('save').setText('Save');
							},
							failure : function(form, action) {
								Ext.Msg.alert('Failed', 'There is some error returned from the server.');
								formPanel.setLoading(false);
							}
						});
					}
				}
			}
		} ],
		listeners : {
			render : function(p) {
				if (App.screenPermiton.getAt(0).data.create == 0)
					Ext.getCmp('save').disable();
				else
					Ext.getCmp('save').enable();
			}
		}
	});
};

Customer.setDataToForm = function(data) {

	var formPanl = Ext.getCmp('customer_form');
	formPanel.setLoading('Loading..', formPanl.getForm());
	customer_id = data.data.id;
	email_store.removeAll();
	bank_account_store.removeAll();
	Ext.Ajax.request({
		url : 'getFundManagerListJson.action',
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			fund_manager_store.loadRawData(obj.fundManagerBeans);
		}
	});

	formPanl.getForm().loadRecord(data);
	Ext.Ajax.request({
		url : 'getBankAccountByCustomer.action',
		params : {
			"customer_id" : customer_id
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			bank_account_store.removeAll();
			bank_account_store.loadRawData(obj.bankAccounts);
		}
	});
	Ext.Ajax.request({
		url : 'getCustomerEmail.action',
		params : {
			"customer_id" : customer_id
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			email_store.removeAll();
			email_store.loadRawData(obj.customerEmailDtos);
		}

	});
	Ext.Ajax.request({
		url : 'getCustomerJointApplicant.action',
		params : {
			"customer_id" : customer_id
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			joint_applicant_store.removeAll();
			joint_applicant_store.loadRawData(obj.customerJointApplicantDtos);
		}
	});
	Ext.Ajax.request({
		url : 'getCustomerContactPerson.action',
		params : {
			"customer_id" : customer_id
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			contact_persons_store.removeAll();
			contact_persons_store.loadRawData(obj.contactPersonsDtos);
		}
	});
	Ext.getCmp('save').setText('Update');
	formPanel.setLoading(false);
};

function State_Select(combo, record, index) {

	var value = combo.getValue();
	var individual = Ext.getCmp('FS_Individual');
	var contact = Ext.getCmp('FS_CONTACTDETAILS');
	var corporate = Ext.getCmp('FS_Corporate');
	var bankDetails = Ext.getCmp('FS_BankAccountDetails');
	var jointApp = Ext.getCmp('FS_JointAppicantDetails');
	var bankAccountGrid = Ext.getCmp('FS_BankAccountGrid');
	var contactperson = Ext.getCmp('FS_ContactPerson');
	var correspondance = Ext.getCmp('FS_Correspondance');
	var bankStore = Ext.getCmp('FS_BankAccountGrid').getStore();
	if (value == 'I') {
		individual.show();
		individual.enable(true);
		contact.show();
		contact.enable(true);
		corporate.hide();
		corporate.disable(true);
		correspondance.show();
		bankDetails.show();
		jointApp.hide();
		contactperson.hide();
		bankAccountGrid.show();
		bankStore.removeAll();
	} else if (value == 'J') {
		individual.show();
		individual.enable(true);
		contact.show();
		contact.enable(true);
		correspondance.show();
		corporate.hide();
		corporate.disable(true);
		bankDetails.show();
		contactperson.hide();
		jointApp.show();
		bankAccountGrid.show();
		bankStore.removeAll();
	} else if (value == 'C') {
		individual.hide();
		individual.disable(true);
		contact.hide();
		contact.disable(true);
		corporate.show();
		corporate.enable(true);
		correspondance.show();
		contactperson.show();
		bankDetails.show();
		jointApp.hide();
		bankAccountGrid.show();
		bankStore.removeAll();
	} else {
		individual.hide();
		contact.hide();
		corporate.hide();
		correspondance.hide();
		contactperson.hide();
		bankDetails.hide();
		jointApp.hide();
		bankAccountGrid.hide();
		bankStore.removeAll();
	}
}
